﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace zUfDsl.Ext
{
    public partial class SuperGrid : System.Windows.Forms.DataGridView
    {
        #region #region 显示按钮

        protected bool datagrideditcontrolhasevents;
        protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
        {
            base.OnEditingControlShowing(e);
            if (!this.datagrideditcontrolhasevents)
            {
                this.EditingControl.Resize += new System.EventHandler(this.datagridviewEditingControlResize);
                this.datagrideditcontrolhasevents = true;
            }
            else
            { datagridviewEditingControlResize(null, null); }
        }

        

        protected void datagridviewEditingControlResize(object sender, EventArgs e)
        {
            object tag = this.Columns[this.CurrentCell.ColumnIndex].Tag;
            Button b;
            Control[] b1 = this.EditingControl.Controls.Find("bref", true);
            if (tag != null)
            {
                foreach (Control c in this.EditingControl.Controls)
                {
                    this.EditingControl.Controls.Remove(c);
                }
                var tagstr = tag.ObjToString();
                switch(tagstr)
                {
                    case "editstring":
                        //if (b1.Length > 0)
                        //{
                        //    b = (Button)b1[0];
                        //}
                        //else
                        //{
                            b = new Button() { Name = "bref", Text = "...", Visible = true };
                            b.Font = new System.Drawing.Font("Arial Rounded MT Bold", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                            b.Size = new System.Drawing.Size(27, 18);
                            b.BackColor = System.Drawing.SystemColors.Control;
                            b.Visible = true;
                            b.Click += new System.EventHandler(this.btrefClick);
                            this.EditingControl.Controls.Add(b);
                        //}
                        b.Location = new System.Drawing.Point(this.EditingControl.Width - 25, this.EditingControl.Height - 16);
                        break;
                    case "editdate":
                        var oldval = ((TextBox)this.EditingControl).Text.ObjToDate();
                        var dtp = new DateTimePicker() {Name="dtp", Left = 0, Top = 0, Width = this.EditingControl.Width, Visible = true };
                        dtp.CustomFormat = "yyyy-MM-dd";
                        dtp.Format = DateTimePickerFormat.Custom;
                        dtp.Value = oldval;
                        dtp.ValueChanged += dateTimePicker1ValueChanged;
                        this.EditingControl.Controls.Add(dtp);
                        break;
                }
                
            }
            else
            {
                if (b1.Length > 0) { this.EditingControl.Controls.Remove(b1[0]); }
            }
        }

        #endregion

        private void dateTimePicker1ValueChanged(object sender, EventArgs e)
        {
            var dtp = (DateTimePicker)sender;
            var parent = (TextBox)dtp.Parent;
            parent.Text = dtp.Value.ToString("yyyy-MM-dd");
        }

        #region 按钮事件

        public delegate void btRefClickHandle(object sender, EventArgs e);
        //定义事件
        public event btRefClickHandle btRefClick;
        protected void btrefClick(object sender, EventArgs e)
        {
            if (btRefClick != null)
            {
                btRefClick(sender, new EventArgs());
            }
        }
        #endregion
    }

}
